package com.cysyz.web.modules.base.init;

import com.cysyz.mylib.utils.MyStringUtils;
import com.cysyz.mylib.utils.PropertiesUtil;
import com.cysyz.web.modules.base.permission.service.PermissionService;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class InitBean implements InitializingBean
{

    @Resource
    private PermissionService permissionService;

    public void afterPropertiesSet()
    {
        privilegeInit();
    }

    private void privilegeInit() {
        Properties properties = PropertiesUtil.getPropertiesFromClasspath("/permission.properties");
        String permissionBeanScanPackage = properties.getProperty("permissionBeanScanPackage");
        String permissioinSwitch = properties.getProperty("permission.switch");

        if (MyStringUtils.hasText(permissioinSwitch) && permissioinSwitch.equalsIgnoreCase("true")) {
            List<String> scanPackage = new ArrayList<String>();
            scanPackage.add("com.cysyz");
            if (MyStringUtils.hasText(permissionBeanScanPackage)) {
                scanPackage.addAll(Arrays.asList(permissionBeanScanPackage.split(",")));
            }
            permissionService.initPrivilege(scanPackage.toArray(new String[]{}));
            permissionService.initGroupGroup();
            permissionService.initAdminRole();
            permissionService.initAdminUser();
        }
    }

}  